﻿using Emgu.CV;
using System;
using System.Windows.Forms;

namespace OpencvProjectBase.TestWinform
{
    public partial class Mainform : Form
    {
        public Mainform()
        {
            InitializeComponent();
        }
        //EmguHelper.EmguBlob m_cv=new EmguHelper.EmguBlob();
        EmguHelper.Blob.AdvanceBlob m_cv = new EmguHelper.Blob.AdvanceBlob();
        private void btnReadImage_Click(object sender, EventArgs e)
        {
            //m_cv.Ts();
            //return;
            string path = "./test1.bmp";
            //Bitmap bmp=new Bitmap(path);
            Mat src = new Mat(path);
            CvInvoke.Imshow("imge", src);
            Mat raw = new Mat();
            if (src.Dims > 1)
                CvInvoke.CvtColor(src, src, Emgu.CV.CvEnum.ColorConversion.Bgr2Gray);
            //CvInvoke.Imshow("gray", src);
            Mat thres = new Mat();
            //CvInvoke.Threshold(src, thres, 180, 255, Emgu.CV.CvEnum.ThresholdType.BinaryInv);

            thres = m_cv.ThresholdMinMax(src, 101, 170);
            CvInvoke.Imshow("thres", thres);
            

            var re = m_cv.Connection(thres, out raw);


            CvInvoke.WaitKey(1);

            //pbxImage.Image = BitmapAndMat.Mat2Bitmap();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string path = "./tentest.bmp";
            Mat src = new Mat(path);
            Mat gray = new Mat();
            CvInvoke.CvtColor(src, gray, Emgu.CV.CvEnum.ColorConversion.Bgr2Gray);
            //CvInvoke.Imshow("gray", gray);

            //二值化
            Mat thres=new Mat();
            CvInvoke.Threshold(gray, thres, 200, 255, Emgu.CV.CvEnum.ThresholdType.Binary);
            //CvInvoke.Imshow("threshold", thres);

            //连通域分离
            Mat connect= new Mat();
            Mat stats= new Mat();
            Mat cent= new Mat();
            CvInvoke.ConnectedComponentsWithStats(thres, connect, stats, cent);
            CvInvoke.Imshow("compoents", connect);
            Console.WriteLine("output");

            CvInvoke.WaitKey(1);

        }
    }
}
